package com.cloud.bc.controller.ajax.center;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cloud.bc.model.domain.UserInfo;
import com.cloud.bc.model.entity.*;
import com.cloud.bc.model.request.LikeRequest;
import com.cloud.bc.model.request.PhotoReq;
import com.cloud.bc.model.response.AjaxResp;
import com.cloud.bc.service.*;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

@AllArgsConstructor
@RestController
@RequestMapping("m/ajax/center")
public class Ajax_MCenterController {

    private final BcUsersService bcUsersService;
    private final BcSinglesService bcSinglesService;
    private final BcUserPhotosService bcUserPhotosService;
    private final BcSingleContactsService bcSingleContactsService;
    private final BcGreespeaksService bcGreespeaksService;
    private final BcLikesService bcLikesService;
    private final BcFootprintsService bcFootprintsService;

    /**
     * 相册
     */
    @PostMapping("photo")
    public AjaxResp photo(PhotoReq req, HttpServletRequest request,
                          HttpServletResponse response) {
        UserInfo userInfo = bcUsersService.getUserInfo(request);
        AjaxResp ajaxResp = new AjaxResp();

        if (Convert.toInt(req.getId(), 0) > 0) {
            //如果有id就删除
            QueryWrapper<BcUserPhotos> bcUserPhotosQueryWrapper = new QueryWrapper<>();
            bcUserPhotosQueryWrapper.lambda().eq(BcUserPhotos::getUserId, userInfo.getId());
            bcUserPhotosQueryWrapper.lambda().eq(BcUserPhotos::getId, req.getId());
            bcUserPhotosService.remove(bcUserPhotosQueryWrapper);
            return ajaxResp;
        }
        //上传
        BcUserPhotos bcUserPhotos = new BcUserPhotos();
        bcUserPhotos.setUserId(userInfo.getId());

        bcUserPhotos.setPhoto(req.getPhoto());
        List<BcUserPhotos> bcUserPhotosResult = bcUserPhotosService.selectAll(bcUserPhotos);
        BcUserPhotos bcUserPhotosFirst = bcUserPhotosResult.stream().findFirst().orElse(new BcUserPhotos());
        if (null != bcUserPhotosFirst) {
            ajaxResp.setObj(JSONUtil.toJsonStr(bcUserPhotosFirst));
        }
        //查询图片相册
        return ajaxResp;
    }

    @PostMapping("contact")
    public AjaxResp contact(HttpServletRequest request,
                            HttpServletResponse response,
                            BcSingleContacts req) {
        UserInfo userInfo = bcUsersService.getUserInfo(request);
        AjaxResp ajaxResp = new AjaxResp();
        req.setIsCheck(3);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id", userInfo.getId());
        bcSingleContactsService.update(req, queryWrapper);
        //查询图片相册
        return ajaxResp;
    }

    @GetMapping("like")
    public AjaxResp like(HttpServletRequest request,
                         HttpServletResponse response,
                         LikeRequest req
    ) {
        UserInfo userInfo = bcUsersService.getUserInfo(request);
        AjaxResp ajaxResp = new AjaxResp();
        Page p = new Page();
        p.setCurrent(req.getPage());
        p.setSize(5);
        BcLikes bcLikes = new BcLikes();
        List<BcSingles> list = null;
        if (req.getType().equals(0)) {
            //我心动的
            list = bcLikesService.selectMyLike(p, userInfo.getId());
        } else if (req.getType().equals(1)) {
            //心动我的
            list = bcLikesService.selectLikeMe(p, userInfo.getId());
        } else if (req.getType().equals(2)) {
            //互相心动的
            list = bcLikesService.selectMutual(p, userInfo.getId());
        }
        if (null != list) {
            list.forEach(item -> {
                item.setAge(bcSinglesService.ageConvert(item.getBirthday()));
            });
        }
        ajaxResp.setList(list);
        return ajaxResp;
    }

    /**
     * 足迹
     */
    @GetMapping("footprint")
    public AjaxResp footprint(HttpServletRequest request,
                              HttpServletResponse response,
                              LikeRequest req
    ) {
        UserInfo userInfo = bcUsersService.getUserInfo(request);
        AjaxResp ajaxResp = new AjaxResp();
        List<BcSingles> list = null;
        Page p = new Page();
        p.setCurrent(req.getPage());
        p.setSize(5);
        if (req.getType().equals(1)) {
            //我看过的
            list = bcFootprintsService.selectMyLook(p, userInfo.getId());
        } else if (req.getType().equals(2)) {
            //看过我的
            list = bcFootprintsService.selectLookMe(p, userInfo.getId());
        }
        if (null != list) {
            list.forEach(item -> {
                item.setAge(bcSinglesService.ageConvert(item.getBirthday()));
            });
        }
        ajaxResp.setList(list);
        return ajaxResp;
    }

}
